using UnityEngine;
using UnityEngine.UI;

public class ChangeTune : MonoBehaviour
{
    public Text Key;
    int moveTune = 0;
    float pitch = 1;
    string[] TuneName = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };

    public void HigherTune()
    {
        moveTune++;
        pitch *= 1.059463f;
        RefreshTuneData();
    }

    public void LowerTune()
    {
        moveTune--;
        pitch /= 1.059463f;
        RefreshTuneData();
    }

    public void OctaveUp()
    {
        moveTune += 12;
        for (int i = 0; i < 12; i++)
        {
            pitch *= 1.059463f;
        }
        RefreshTuneData();
    }

    public void OctaveDown()
    {
        for (int i = 0; i < 12; i++)
        {
            pitch /= 1.059463f;
        }
        moveTune -= 12;
        RefreshTuneData();
    }

    void RefreshTuneData()
    {
        GameObject tmp;
        if (moveTune >= 0)
        {
            Key.text = TuneName[moveTune % 12] + " +" + moveTune / 12;
            if(moveTune == 0)
            {
                pitch = 1;
            }
        }
        else
        {
            Key.text = TuneName[-moveTune % 12] + " -" + -moveTune / 12;
        }
        for (int i = 0; i < 21; i++)
        {
            tmp = GameObject.Find((i / 7).ToString() + (i % 7).ToString());
            tmp.GetComponent<AudioSource>().pitch = pitch;
        }
    }
}
